package cn.lagou

import scala.collection.mutable

object Question3 {
  def main(args: Array[String]): Unit = {
    merger()
  }

  def merger(): Unit = {
    val data = Array(
      "UserA,LocationA,8,60",
      "UserA,LocationA,9,100",
      "UserB,LocationB,10,60",
      "UserB,LocationB,11,80"
    )

    val newData = mutable.Map[String, UserInfo]()

    data.foreach(user => {
      val userInfos = user.split(",")
      if (userInfos.length < 4) {
        println("数据异常")
      } else {
        val newUserInfo = new UserInfo(userInfos(0), userInfos(1), userInfos(2), userInfos(3))

        val userLocal = newUserInfo.name + newUserInfo.location

        val user = newData.getOrElse(userLocal, null)
        if (user != null) {
          if (user.startTime.toInt > newUserInfo.startTime.toInt) user.startTime = newUserInfo.startTime

          if (user.stayTime.toInt < newUserInfo.stayTime.toInt) user.stayTime =  (user.stayTime.toInt + newUserInfo.stayTime.toInt).toString
        } else {
          newData(userLocal) = newUserInfo
        }
      }
    })

    newData.foreach(e => println(e._2.toString))
  }
}

class UserInfo {
  var name: String = _
  var location: String = _
  var startTime: String = _
  var stayTime: String = _

  def this(name: String, location: String, startTime: String, stayTime: String) {
    this()
    this.name = name
    this.location = location
    this.startTime = startTime
    this.stayTime = stayTime
  }

  override def toString: String = {
    this.name + "," + this.location + "," + this.startTime + "," + this.stayTime
  }
}